package cn.xjfme.encrypt.dto;


import lombok.Data;

@Data
public class ResultResponse<T> {
    private Integer code;
    private String message;
    private String date;
    private T data;
    private Boolean success;

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getDate() {
        return date;
    }

    public T getData() {
        return data;
    }


    public ResultResponse() {
        super();
    }

    public ResultResponse(Integer code, String message, String date, T data) {
        super();
        this.code = code;
        this.message = message;
        this.date = date;
        this.data = data;
        this.success = true;
    }


    /**
     * 成功返回值
     *
     * @param data
     * @return
     */
    public ResultResponse<T> buildSuccessResult(T data) {
        return new BuliderResultResponse<T>(ReturnCodeEnum.C200_.getCode(), ReturnCodeEnum.C200_.getMessage(), data).builder();
    }

    /**
     * 指定返回码
     *
     * @param data
     * @return
     */
    public ResultResponse<T> buildResult(ReturnCodeEnum returnCodeEnum, T data) {
        return new BuliderResultResponse<T>(returnCodeEnum.getCode(), returnCodeEnum.getMessage(), data).builder();
    }


    /**
     * 指定返回码-消息
     *
     * @param data
     * @return
     */
    public ResultResponse<T> buildResult(ReturnCodeEnum returnCodeEnum, String message, T data) {
        return new BuliderResultResponse<T>(returnCodeEnum.getCode(), message == null || message.trim().length() == 0 ? returnCodeEnum.getMessage() : message, data).builder();
    }

    /**
     * 失败返回值
     *
     * @param data
     * @return
     */
    public ResultResponse<T> buildFailResult(ReturnCodeEnum returnCodeEnum, String message) {
        return new BuliderResultResponse<T>(returnCodeEnum.getCode(), message == null ? returnCodeEnum.getMessage() : message, null).builder();
    }

    public static <T> ResultResponse<T> success(T data) {
        return success(data, "操作成功");
    }

    public static <T> ResultResponse<T> success(T data, String message) {
        BuliderResultResponse<T> result = new BuliderResultResponse();
        result.setData(data);
        result.setMessage(message);
        result.setDate(String.valueOf(System.currentTimeMillis()));
        result.setCode(ReturnCodeEnum.C200_.getCode());
        result.setSuccess(true);
        return result.builder();
    }

    public static <T> ResultResponse<T> failure(Integer errorCode, String message) {
        return failure(errorCode, message, null);
    }

    public static <T> ResultResponse<T> failure(Integer errorCode, String message, T errorData) {
        BuliderResultResponse<T> result = new BuliderResultResponse();
        result.setCode(errorCode);
        result.setData(errorData);
        result.setMessage(message);
        result.setSuccess(false);
        return result.builder();
    }

    public static class BuliderResultResponse<T> {


        private Integer code;
        private String message;
        private String date;
        private T data;
        private Boolean success;

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public Boolean getSuccess() {
            return success;
        }

        public void setSuccess(Boolean success) {
            this.success = success;
        }

        public BuliderResultResponse() {
            super();
        }

        public BuliderResultResponse(Integer code, String message, String date, T data) {
            super();
            this.code = code;
            this.message = message;
            this.date = date;
            this.data = data;
        }

        public BuliderResultResponse(Integer code, String message, T data) {
            super();
            this.code = code;
            this.message = message;
            this.date = System.currentTimeMillis() + "";
            this.data = data;
        }

        public ResultResponse<T> builder() {
            return new ResultResponse<>(this.code, this.message, this.date, this.data);
        }
    }

    public boolean isSuccess() {
        return this.getCode() == ReturnCodeEnum.C200_.getCode();
    }
}
